package com.common.core.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.core.R;

/**
 * Created by Austin on 16/3/4.
 */
public class SpTools {
    /**
     * shared_preference文件名
     */
    private static final String sharedPreferenceName = "lajin_config";
    private static int mode = Context.MODE_PRIVATE;
    private static SharedPreferences sp;
//    public static final String first_enter = "first_enter";

    public static Object getValue(Context c, String key, Object value) {
        if (sp == null) {
            sp = c.getSharedPreferences(sharedPreferenceName, mode);

        }
        if (value instanceof Boolean)
            return sp.getBoolean(key, (Boolean) value);
        else if (value instanceof Integer)
            return sp.getInt(key, (Integer) value);
        else if (value instanceof Float)
            return sp.getFloat(key, (Float) value);
        else if (value instanceof Long)
            return sp.getLong(key, (Long) value);
        else if (value instanceof String)
            return sp.getString(key, (String) value);
        else
            return "";
    }

    public static Object saveValue(Context c, String key, Object value) {
        if (sp == null) {
            sp = c.getSharedPreferences(sharedPreferenceName, mode);

        }
        SharedPreferences.Editor ed = sp.edit();
        if (value instanceof Boolean)
            return ed.putBoolean(key, (Boolean) value).commit();
        else if (value instanceof Integer)
            return ed.putInt(key, (Integer) value).commit();
        else if (value instanceof Float)
            return ed.putFloat(key, (Float) value).commit();
        else if (value instanceof Long)
            return ed.putLong(key, (Long) value).commit();
        else if (value instanceof String)
            return ed.putString(key, (String) value).commit();
        else
            return false;
    }

    public static void saveString(Context c, String key, String value) {
        if (sp == null) {
            sp = c.getSharedPreferences(sharedPreferenceName, mode);

        }
        SharedPreferences.Editor ed = sp.edit();
        ed.putString(key, value).apply();
    }

    public static String getString(Context c, String key, String value) {
        if (sp == null) {
            sp = c.getSharedPreferences(sharedPreferenceName, mode);

        }
        return sp.getString(key, value);
    }


    public static void saveLong(Context c, String key, Long value) {
        if (sp == null) {
            sp = c.getSharedPreferences(sharedPreferenceName, mode);

        }
        SharedPreferences.Editor ed = sp.edit();
        ed.putLong(key, value).apply();
    }

    public static Long getLong(Context c, String key, Long value) {
        if (sp == null) {
            sp = c.getSharedPreferences(sharedPreferenceName, mode);

        }
        return sp.getLong(key, value);
    }

    public static void clearData(Context c) {
        if (sp == null) {
            sp = c.getSharedPreferences(sharedPreferenceName, mode);

        }
        SharedPreferences.Editor editor = sp.edit();
        editor.clear().apply();
    }

    //通过key来删除对应的缓存值
    public static void removeData(Context c, String key) {
        if (sp == null) {
            sp = c.getSharedPreferences(sharedPreferenceName, mode);

        }
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key).apply();
    }

    public static String getStarMarkerPath(Context context, int value) {
        return SpTools.getString(context, "star_marker_value_" + value + "_path", null);
    }

    public static void saveStarMarketPath(Context context, int value, String starMarketPath) {
        SpTools.saveString(context, "star_marker_value_" + value + "_path", starMarketPath);
    }

    public static void removeStarMarketPath(Context context, int value) {
        SpTools.removeData(context, "star_marker_value_" + value + "_path");
    }

    public static void setStarMarkerImageIcon(Context context, final ImageView imageView, final int starmarker, final int resId) {
        if (imageView != null) {
            String starUri = getStarMarkerPath(context, starmarker);
            if (StringUtils.isEmpty(starUri)) {
                if (resId > 0) {
                    imageView.setVisibility(View.VISIBLE);
                    imageView.setImageResource(resId);
                } else {
                    imageView.setVisibility(View.GONE);
                }
            } else {
                imageView.setVisibility(View.VISIBLE);
                imageView.setImageURI(Uri.parse(starUri));
            }
        }
    }

    public static void setStarMarkerImageIcon(Context context, final TextView textView, final int starmarker, final int resId) {
        if (textView != null) {
            String starUri = getStarMarkerPath(context, starmarker);
            if (StringUtils.isEmpty(starUri)) {
                if (resId > 0) {
                    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId);
                    BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bitmap);
                    bitmapDrawable.setBounds(0, 0, context.getResources().getDimensionPixelSize(R.dimen.dimen_32_dip), context.getResources().getDimensionPixelSize(R.dimen.dimen_32_dip));//必须设置图片大小，否则不显示
                    textView.setCompoundDrawables(null, null, bitmapDrawable, null);
                }
            } else {
                Bitmap bitmap = BitmapFactory.decodeFile(starUri);
                BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bitmap);
                bitmapDrawable.setBounds(0, 0, context.getResources().getDimensionPixelSize(R.dimen.dimen_32_dip), context.getResources().getDimensionPixelSize(R.dimen.dimen_32_dip));//必须设置图片大小，否则不显示
                textView.setCompoundDrawables(null, null, bitmapDrawable, null);
            }
        }
    }

    public static void setTextViewDrawable(Context context, TextView textView, int resId) {
        if (resId > 0) {
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId);
            BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bitmap);
            bitmapDrawable.setBounds(0, 0, context.getResources().getDimensionPixelSize(R.dimen.dimen_26_dip), context.getResources().getDimensionPixelSize(R.dimen.dimen_26_dip));//必须设置图片大小，否则不显示
            textView.setCompoundDrawables(null, null, bitmapDrawable, null);
        } else {
            textView.setCompoundDrawables(null, null, null, null);
        }
    }
}
